#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<vector>
using namespace std;

vector<int> get_next(string& s)
{
	int i = 2;
	int k = 0;
	int len = s.length();
	vector<int> next(len, -2);
	next[0] = -1;
	next[1] = 0;
	while (i < len)
	{
		if (k == -1 || s[i - 1] == s[k])
		{
			k++;
			next[i] = k;
			i++;
		}
		else
		{
			k = next[k];
		}
	}
	return next;
}

int CountKMP(string& s, string& p)
{
	vector<int> next = get_next(p);
	int i = 0;
	int j = 0;
	int count = 0;
	int slen = s.length();
	int plen = p.length();
	while (i < slen)
	{
		if (j >= plen)
		{
			count++;
			j -= 5;
		}
		else if (j == -1 || s[i] == p[j])
		{
			i++;
			j++;
		}
		else
		{
			j = next[j];
		}
	}
	if (j >= plen)
	{
		count++;
	}
	return count;
}

int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		string s, p;
		cin >> s;
		int m;
		cin >> m;
		while (m--)
		{
			p += "edgnb";
		}
		int count = CountKMP(s, p);
		cout << count << endl;
		
	}
	return 0;
}